/**
 * Title:        字段属性检测
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilderFactory;

import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.json.JSONObject;

import weaver.conn.RecordSet;
import weaver.general.Util;



public class BrowserConfigCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        
        (new BaseBean()).writeLog("BrowserConfigCheck  start");

        String result = resultOk;
        Map<String,String> browserTagMap = getServersByName("taglib","taglib-uri","/browserTag");
        Map<String,String> browserMap = getServersByName("taglib","taglib-uri","/browser");
        List resultList = new ArrayList();
        String tempConfig = "";
        tempConfig += " <jsp-config>\n";
        tempConfig += "     <taglib>\n";
        tempConfig += "         <taglib-uri>/browserTag</taglib-uri>\n";
        tempConfig += "         <taglib-location>/WEB-INF/tld/browser.tld</taglib-location>\n";
        tempConfig += "     </taglib>\n";
        tempConfig += "     <taglib>\n";
        tempConfig += "         <taglib-uri>/browser</taglib-uri>\n";
        tempConfig += "         <taglib-location>/WEB-INF/tld/browser.tld</taglib-location>\n";
        tempConfig += "     </taglib>\n";
        tempConfig += " </jsp-config>\n";
        tempConfig = Util.forHtml(tempConfig);

        if(!browserTagMap.get("parElement").equals("jsp-config")){
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            String showMessage = "Tag browserTag的父节点不是jsp-config，请参照以下内容对WEB-INF/web.xml进行配置后重启服务<br>";
            showMessage += tempConfig;
            resultMap.put("showMessage", showMessage);
            resultList.add(resultMap);
        }else if(!browserMap.get("parElement").equals("jsp-config")){
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            String showMessage = "Tag browser的父节点不是jsp-config，请参照以下内容对WEB-INF/web.xml进行配置后重启服务<br>";
            showMessage += tempConfig;
            resultMap.put("showMessage", showMessage);
            resultList.add(resultMap);
        }else if(browserTagMap.get("taglib-location") == null){
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            String showMessage = "Tag browserTag没有配置，请参照以下内容对WEB-INF/web.xml进行配置后重启服务<br>";
            showMessage += tempConfig;
            resultMap.put("showMessage", showMessage);
            resultList.add(resultMap);
        }else if(!browserTagMap.get("taglib-location").equals("/WEB-INF/tld/browser.tld")){
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            String showMessage = "Tag browserTag配置不正确，请参照以下内容对WEB-INF/web.xml进行配置后重启服务<br>";
            showMessage += tempConfig;
            resultMap.put("showMessage", showMessage);
            resultList.add(resultMap);
        }else if(browserMap.get("taglib-location") == null){
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            String showMessage = "Tag browser没有配置，请参照以下内容对WEB-INF/web.xml进行配置后重启服务<br>";
            showMessage += tempConfig;
            resultMap.put("showMessage", showMessage);
            resultList.add(resultMap);
        }else if(!browserMap.get("taglib-location").equals("/WEB-INF/tld/browser.tld")){
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            String showMessage = "Tag browser配置不正确，请参照以下内容对WEB-INF/web.xml进行配置后重启服务<br>";
            showMessage += tempConfig;
            resultMap.put("showMessage", showMessage);
            resultList.add(resultMap);
        }
        (new BaseBean()).writeLog("BrowserConfigCheck  end");
        return formatReturnJsonCheck(result, resultList,this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {
        return checkData(dataMap);
    }

    @Override
    public String getClassName() {
        return "升级E8后浏览按钮配置检测";
    }

    @Override
    public String getClassDesc() {
        return "检测升级E8后，web.xml中是否配置了浏览按钮的设置";
    }

    private Map<String,String> getServersByName(String code,String eleName,String eleValue) {
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        path = path.substring(0,path.length() - 1);
        path = path.substring(0,path.lastIndexOf("/"));
        Thread.currentThread().getContextClassLoader().getResource("").getPath();
        Map map = new HashMap<String,String>();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            File xmlFIle = new File(path + "/web.xml");
            SAXReader sr = new SAXReader();// 获取读取方式
            InputStream in = new FileInputStream(path + "/web.xml");
            org.dom4j.Document doc = sr.read(in);
         // 读取xml文件，并且将数据全部存放到Document中
            Element root = doc.getRootElement();// 获取根节点

            Element root1 = root.element("jsp-config");// 根据根节点，将根节点下
            List xmlList = root1.elements(code);// 根据根节点，将根节点下
                                                        // row中的所有数据放到list容器中。
            if(xmlList != null && xmlList.size() > 0){
                Element parElement = ((Element)xmlList.get(0)).getParent();
                map.put("parElement",parElement.getName());
                for (Object obj : xmlList) {
                    Element row = (Element) obj;
                    row.getNodeTypeName();
                    row.getStringValue();
                    if(row.elements().size() > 0){
                        Element firstClildElement = (Element)(row.elements().get(0));
                        if(!eleName.equals(firstClildElement.getName()) || !eleValue.equals(firstClildElement.getStringValue())){
                            continue;
                        }
                        for(int i = 0; i <row.elements().size(); i ++){
                            Element cell = (Element)row.elements().get(i);
                            map.put(cell.getName(), cell.getStringValue());
                        }
                    }
                }
            }

        } catch (Exception e) {
            writeLog(e.getMessage());
        }
        return map;
    }



}